package com.crm.services;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.model.jdao.ICareDAO;
import com.crm.model.mdao.CareMapper;
import com.crm.model.pojo.Care;
import com.crm.model.pojo.Timeline;
import com.crm.util.LoginUtil;
import com.github.pagehelper.PageHelper;

@Service
public class CareServices {
	@Autowired
	CareMapper careMapper;
	@Autowired
	ICareDAO careDAO;
	@Autowired
	TimeLineServices timeLineServices;
	
	public List<Map<String, String>> selectall() {
		return careMapper.selectAll();
	}
	
	//当日关怀量
	public int selectcareNum() {
		return careMapper.selectCareNum();
	}
	
	/**
	 * 导出
	 * @return
	 */
	public List<Map<String, String>> selectallInfo() {
		return careMapper.selectAllInfo(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId());
	}
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectPage(int pageNum, int pageSize,Integer statueID,String theme) {
		// 使用分页插件
		System.out.println("页码:" + pageNum + ",页数:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		Integer classity = null;
		//对状态进行判断
		if (statueID!=null) {
			if (statueID==1071 || statueID==1072 || statueID==1073) {
				classity=statueID;
			}
		}
		System.out.println("编号："+statueID+"\t分类："+classity);
		// 获取记录数
		List<Map<String, String>> list = careMapper.selectAllNew(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId(),classity,theme);
		System.out.println("list :"+list);
	    return list;
	}
	
	/**
	 * 单条查询
	 * @return 
	 */
	public Map<String, String> selectByid(Integer complaintId) {
		return careMapper.selectByid(complaintId);
	}
	
	/**
	 * 添加客户关怀
	 */
	public Care addCare(Care care) {
		Care save = careDAO.save(care);
		Timeline timeline=new Timeline();
		timeline.setTheme("客户关怀");
		timeline.setTimes(new Date());
		timeline.setCustId(save.getCustomer().getCustId());
		timeline.setDetails("客户："+save.getCustomer().getCustName()+" 关怀主题为:--"+save.getCareTheme());
		timeLineServices.insertTimeLine(timeline);
		System.out.println(save);
		return save;
	}
	
	/**
	 * 添加客户关怀(自动生成)
	 */
	public int addCares(com.crm.model.mpojo.Care care) {
		int insert = careMapper.insert(care);
		return insert;
	}
	
	/**
	 * 物理删除（更改状态）
	 * @return 
	 */
	public int deleteByStatue(Integer complaintId) {
		return careMapper.deleteByStatue(complaintId);
	}
}
